﻿using ExcelSystem.Model;
using ExcelSystem.ToolClass;
using ExcelSystem.ToolHelper;
using ExcelSystem.ViewModelsClass;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace ExcelSystem.ViewModels
{
    public class MaterialFilterViewModel : BindableBase, IDialogAware
    {

        ExcelContext db;
        public string Title => "物料筛选";

        public event Action<IDialogResult> RequestClose;


        private List<Filter> data;
        public List<Filter> Data
        {
            get { return data; }
            set { SetProperty(ref data, value); }
        }

        private List<string> companyList;
        /// <summary>
        /// 公司列表
        /// </summary>
        public List<string> CompanyList
        {
            get { return companyList; }
            set { SetProperty(ref companyList, value); }
        }

        private List<KeyValue<string>> nameList;
        /// <summary>
        /// 字段列表
        /// </summary>
        public List<KeyValue<string>> NameList
        {
            get { return nameList; }
            set { SetProperty(ref nameList, value); }
        }

        private string company;
        /// <summary>
        /// 公司
        /// </summary>
        public string Company
        {
            get { return company; }
            set { 
                SetProperty(ref company, value);
                //更新筛选对象
                ChangeNameList(value);
            }
        }

        private KeyValue<string> name;
        /// <summary>
        /// 要筛选的字段
        /// </summary>
        public KeyValue<string> Name
        {
            get { return name; }
            set { SetProperty(ref name, value); }
        }

        private FilterValue filterStr;
        /// <summary>
        ///  过滤字段
        /// </summary>
        public FilterValue FilterStr
        {
            get { return filterStr; }
            set { SetProperty(ref filterStr, value); }
        }

        private string vagueValue;
        /// <summary>
        /// 模糊值
        /// </summary>
        public string VagueValue
        {
            get { return vagueValue; }
            set { SetProperty(ref vagueValue, value); }
        }

        private string accurateValue;
        /// <summary>
        /// 精确值
        /// </summary>
        public string AccurateValue
        {
            get { return accurateValue; }
            set { SetProperty(ref accurateValue, value); }
        }

        private string headValue;
        /// <summary>
        /// 主梁顶置
        /// </summary>
        public string HeadValue
        {
            get { return headValue; }
            set { SetProperty(ref headValue, value); }
        }


        private Filter dataClass;
    


        private DelegateCommand saveCommand;
        /// <summary>
        /// 保存修改
        /// </summary>
        public DelegateCommand SaveCommand
        {
            get { return saveCommand; }
            set { SetProperty(ref saveCommand, value); }
        }

        private DelegateCommand deteleCommand;
        /// <summary>
        /// 删除数据
        /// </summary>
        public DelegateCommand DeteleCommand
        {
            get { return deteleCommand; }
            set { SetProperty(ref deteleCommand, value); }
        }

        private DelegateCommand filterOrderDataCommand;
        /// <summary>
        /// 删除数据
        /// </summary>
        public DelegateCommand FilterOrderDataCommand
        {
            get { return filterOrderDataCommand; }
            set { SetProperty(ref filterOrderDataCommand, value); }
        }
        




        MaterialFilterViewModel()
        {
            //数据库文档
            db = ExcelContentFactory.GetInstance();
            SaveCommand = new DelegateCommand(Save);
            DeteleCommand = new DelegateCommand(Detele);
            FilterOrderDataCommand = new DelegateCommand(FilterOrderData);
            


            Data = new List<Filter>();
            //基础公式
          
            
            Init();
      

        }

      

        private void Init()
        {
            //确定筛选的公司
            companyList= new List<string>() {
            "西奥","1","2"
            };
            //默认为西奥
            Company = companyList[0];


        }

        private void ChangeNameList(string company) {


            if (company == null)
                return;
            //获取所有筛选项
            Data = db.Filters.Where(t => t.Company == Company).ToList();
            if(Data!=null&&Data.Count>=0)
            {
                //转成控件值
                NameList = new List<KeyValue<string>>();
                foreach (var item in Data)
                {
                    NameList.Add(JsonConvert.DeserializeObject<KeyValue<string>>(item.Name));
                }
                //默认第一项
                if(Data.Count > 0)
                dataClass = Data[0];
                //展示第一项的值
                if (NameList.Count > 0)
                    Name = NameList[0];
                FilterStr = new FilterValue();
                if(dataClass!=null)
                FilterStr = JsonConvert.DeserializeObject<FilterValue>(dataClass.Value);
            }
           
        }

        /// <summary>
        /// 保存筛选修改
        /// </summary>
        private void Save()
        {
            if (!string.IsNullOrEmpty(VagueValue)&& FilterStr.VagueName.Contains(VagueValue))
            {
                MessageBox.Show("已存在相同的模糊值，请勿重复添加");
            }
            else if(!string.IsNullOrEmpty(VagueValue))
            {
                FilterStr.VagueName.Add(VagueValue);
            }

            if (!string.IsNullOrEmpty(AccurateValue) && FilterStr.AccurateName.Contains(AccurateValue))
            {
                MessageBox.Show("已存在相同的精确值，请勿重复添加");
            }
            else if(!string.IsNullOrEmpty(AccurateValue) )
            {
                FilterStr.AccurateName.Add(AccurateValue);
            }

            if (!string.IsNullOrEmpty(HeadValue) && FilterStr.OverHead.Contains(HeadValue))
            {
                MessageBox.Show("已存在相同的精确值，请勿重复添加");
            }
            else if (!string.IsNullOrEmpty(HeadValue))
            {
                FilterStr.OverHead.Add(HeadValue);
            }


            
            dataClass.Value = JsonConvert.SerializeObject(FilterStr);
            db.SaveChanges();
            VagueValue = "";
            AccurateValue = "";
            HeadValue = "";

        }

        private void Detele()
        {
          
            if (!string.IsNullOrEmpty(VagueValue))
            {
                FilterStr.VagueName.Remove(VagueValue);
            }

          
            if (!string.IsNullOrEmpty(AccurateValue))
            {
                FilterStr.AccurateName.Remove(AccurateValue);
            }

            if (!string.IsNullOrEmpty(HeadValue))
            {
                FilterStr.OverHead.Remove(HeadValue);
            }


            dataClass.Value = JsonConvert.SerializeObject(FilterStr);
            db.SaveChanges();
            VagueValue = "";
            AccurateValue = "";
            HeadValue = "";
        }

        /// <summary>
        /// 删除订单中数据
        /// </summary>
        private void FilterOrderData()
        {
            //查询所有符合条件的物料
            //获取所有ID
            var allNo = db.Materials.Select(t => t.MaterialNo).ToList();
            //对ID进行筛选
            var result = allNo.Where(t => {
                bool result=true;
                foreach (var va in filterStr.VagueName)
                {
                    result = (result &&!t.Contains(va));

                }
                foreach (var va in filterStr.AccurateName)
                {
                    result = (result && t!=va);

                }
                return result;
                 
            }).ToList();
            allNo = allNo.Where(t=> !result.Contains(t)).ToList();





        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            
           
        }
        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }
    }
}
